package treeMap.test3;

import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class Test {
    public static void main(String[] args) {
        //通过treemap进行统计
        String str = "abbcccdddd";
        //创建treemap集合
        TreeMap<Character, Integer> ts = new TreeMap<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (ts.containsKey(c)) {
                //已经存在了 value +1
                int count = ts.get(c);
                count++;
                ts.put(c, count);
            } else {
                ts.put(c, 1);
            }

        }
        //进行遍历
        StringBuilder sb = new StringBuilder();
        ts.forEach(new BiConsumer<Character, Integer>() {
            @Override
            public void accept(Character key, Integer value) {
                sb.append(key + "(" + value + ")");
            }
        });
        System.out.println(ts);
        System.out.println(sb);
        //创建StringJoiner
        StringJoiner sj = new StringJoiner("");
        ts.forEach((key, value) -> sj.add(key + "(").add(value + ")"));
        System.out.println(sj);
    }


}
